#include <stdio.h>
#include<stdlib.h>
//下面两个头文件基本都会用到，别省了
#include "cuda_runtime.h"
#include "device_launch_parameters.h"

//这个宏还是蛮有用,主要功能是检测cuda函数的错误，以方便检查
/*
  1.cudaError_t:CUDA的错误码，它的错误码太多了，大概有80-90个，具体什么错误碰到了再解释
  2.cudaGetErrorString():将CUDA错误码转换成具体错误语句，方便判断程序员错误
  3.__FILE__,__LINE__:这个就不解释了，虽然我也用的不太熟
*/
#define CHECK(call)                                                  \
{                                                                    \
    const cudaError_t err = call;                                    \
    if (err != cudaSuccess)                                          \
    {                                                                \
        fprintf(stderr, "Error: %s:%d, ", __FILE__, __LINE__);       \
        fprintf(stderr, "code: %d, reason: %s\n", err,               \
                cudaGetErrorString(err));                            \
        exit(1);                                                     \
    }                                                                \
}




/*checkDimension.cu：显示线程的维度 */
/*Authored by alpc40*/
__global__ void checkIndex(void)
{
    printf("threadIdx:(%d, %d, %d)\n", threadIdx.x, threadIdx.y, threadIdx.z);
    printf("blockIdx:(%d, %d, %d)\n", blockIdx.x, blockIdx.y, blockIdx.z);
    printf("blockDim:(%d, %d, %d)\n", blockDim.x, blockDim.y, blockDim.z);
    printf("gridDim:(%d, %d, %d)\n", gridDim.x, gridDim.y, gridDim.z);
}

int main2(int argc, char** argv)
{
    int nElem = 6;//定义总计算量
    dim3 block(3);// 定义grid和block
    dim3 grid((nElem + block.x - 1) / block.x);// 定义grid和block
    printf("grid.x %d grid.y %d grid.z %d\n", grid.x, grid.y, grid.z);// CPU端检测维度
    printf("block.x %d block.y %d block.z %d\n", block.x, block.y, block.z);
    checkIndex << <grid, block >> > ();// GPU端检测维度
    CHECK(cudaDeviceReset());// 恢复GPU
    return 0;
}